package com.example.Windturbine.Service.Impl;

import com.example.Windturbine.Dao.ManufactureDOMapper;
import com.example.Windturbine.Dao.ManufactureInfoDOMapper;
import com.example.Windturbine.DataObject.ManufactureDO;
import com.example.Windturbine.DataObject.ManufactureInfoDO;
import com.example.Windturbine.DataObject.TransportDO;
import com.example.Windturbine.Error.BusinessException;
import com.example.Windturbine.Error.EmBusinessError;
import com.example.Windturbine.Service.ManufactureService;
import com.example.Windturbine.Service.Model.ManufactureModel;
import com.example.Windturbine.Service.Model.MaterialModel;
import com.example.Windturbine.Service.Model.TransportModel;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author 丁永新
 * @Date 2021/12/20
 */
@Service
public class ManufactureServiceImpl implements ManufactureService {

    @Resource
    ManufactureDOMapper manufactureDOMapper;

    @Resource
    ManufactureInfoDOMapper manufactureInfoDOMapper;

    @Override
    public List<ManufactureDO> getAll(Integer level) throws BusinessException {
        try{
            List<ManufactureDO> all = manufactureDOMapper.getAll(level);
            if(all==null){
                throw new BusinessException(EmBusinessError.UNKNOWN_ERROR,"查询失败！");
            }
            return all;
        }catch (Exception e){
            throw new BusinessException(EmBusinessError.UNKNOWN_ERROR,"获取工艺数据失败！");
        }

    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, rollbackFor = Exception.class)
    public void submitSingle(ManufactureModel manufactureModel) throws BusinessException {
        try {
            ManufactureDO manufactureDO = convertFromModel(manufactureModel);
            int i = manufactureDOMapper.insertSelective(manufactureDO);
            manufactureModel.setId(manufactureDO.getId());
            ManufactureInfoDO manufactureInfoDO = convertFromModel2Info(manufactureModel);
            int i1 = manufactureInfoDOMapper.insertSelective(manufactureInfoDO);
            if(i==0||i1==0){
                throw new BusinessException(EmBusinessError.UNKNOWN_ERROR,"插入失败！");
            }
        } catch (Exception e) {
            throw new BusinessException(EmBusinessError.UNKNOWN_ERROR, "提交工艺数据失败！");
        }
    }

    @Override
    public ManufactureModel getDetailById(Integer manufacture_id) throws BusinessException {
        try {
            ManufactureInfoDO manufactureInfoDO = manufactureInfoDOMapper.selectByManufactureID(manufacture_id);
            ManufactureDO manufactureDO = manufactureDOMapper.selectByPrimaryKey(manufacture_id);
            if(manufactureInfoDO==null||manufactureDO==null){
                throw new BusinessException(EmBusinessError.UNKNOWN_ERROR,"查询失败！");
            }
            ManufactureModel manufactureModel = convertFromInfo(manufactureInfoDO);
            manufactureModel.setPercent(manufactureDO.getPercent());
            return manufactureModel;
        } catch (Exception e) {
            throw new BusinessException(EmBusinessError.UNKNOWN_ERROR, "查看工艺详情数据失败！");
        }
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, rollbackFor = Exception.class)
    public void deleteById(Integer id) throws BusinessException {
        try {
            Integer n = manufactureDOMapper.deleteByPrimaryKey(id);
            if (n != 0){
                manufactureInfoDOMapper.deleteByManufactureId(id);
            }else{
                throw new BusinessException(EmBusinessError.UNKNOWN_ERROR, "删除工艺详情数据失败！系统数据无法删除！");
            }
        }catch (Exception e) {
            throw new BusinessException(EmBusinessError.UNKNOWN_ERROR, "删除工艺详情数据失败！");
        }
    }


    @Override
    public ManufactureModel getDetailByName(String tran_name, String material_name,Integer level) throws BusinessException{
        List<ManufactureDO> all = getAll(level);
        try{
            List<ManufactureDO> transportDOS = new ArrayList<>();
            for(ManufactureDO m:all){
                if(m.getName().equals(tran_name)&&m.getMaterial().equals(material_name)) transportDOS.add(m);
            }
            ManufactureDO first=transportDOS.get(0);
            ManufactureModel res=getDetailById(first.getId());
            if(res==null) throw new BusinessException(EmBusinessError.UNKNOWN_ERROR,"工艺"+tran_name+"信息缺失!");
            return res;
        }catch (Exception e){
            throw new BusinessException(EmBusinessError.UNKNOWN_ERROR,"工艺"+tran_name+"信息缺失!");
        }

    }



    private ManufactureDO convertFromModel(ManufactureModel manufactureModel){
        ManufactureDO manufactureDO=new ManufactureDO();
        BeanUtils.copyProperties(manufactureModel,manufactureDO);
        return manufactureDO;
    }

    private ManufactureInfoDO convertFromModel2Info(ManufactureModel manufactureModel){
        ManufactureInfoDO manufactureInfoDO=new ManufactureInfoDO();
        BeanUtils.copyProperties(manufactureModel,manufactureInfoDO);
        manufactureInfoDO.setManufactureId(manufactureModel.getId());
        return manufactureInfoDO;
    }

    private ManufactureModel convertFromInfo(ManufactureInfoDO manufactureInfoDO){
        ManufactureModel manufactureModel=new ManufactureModel();
        BeanUtils.copyProperties(manufactureInfoDO,manufactureModel);
        return manufactureModel;
    }
}
